package com.jm.web.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * @author yanjie
 * Nov 18, 2011 4:35:20 PM
 */
public class Mail {
	public static final int GET = 0; // Get method.
	public static final int POST = 1; // Post method.
    
	public static final int URL = 2;
    public static final int TEXT = 3;
    public static final int HTML = 4;
    
	private String from 	= "exchgadmin@mail.JMKNIT.com";//发件人
	private String to		= "" ;//收件人
	private String cc		= "" ;//副本
	private String bcc		= "" ;//密本
	private String subject 	= "" ;//标题
	private String body 	= "" ;//邮件主体
	private int contentType	= 4 ;//邮件类型
	private boolean bAuth = false;
	private boolean bEncode = true;
	
	private String fileName = "";//附件名称
	private File file = null;//附件
	
	public String getResponse(String url, int method, NameValuePair... params) throws Exception {
        StringBuffer response = new StringBuffer();
        HttpClient client = new HttpClient(); // HTTPClient.
        String encoing = "UTF-8"; // charset encoding.
        int statusCode = -1;
        BufferedReader br;
        String line;
        if (method == GET) {
            GetMethod get = new GetMethod(url);
            statusCode = client.executeMethod(get);
            if (statusCode != -1) {
                br = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(), encoing));
                while ((line = br.readLine()) != null) {
                    response.append(line).append(System.getProperty("line.separator"));
                }
            }
            get.releaseConnection();
            return response.toString();
        } else if (method == POST) {
            PostMethod post = new PostMethod(url);
            post.setRequestBody(params);
            statusCode = client.executeMethod(post);
            if (statusCode != -1) {
                br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), encoing));
                while ((line = br.readLine()) != null) {
                    response.append(line).append(System.getProperty("line.separator"));
                }
            }
            post.releaseConnection();
            return response.toString();
        } else {
            throw new Exception("Method getResponse() occur Exception : ");
        }
    }
	
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public String getCc() {
		return cc;
	}
	public void setCc(String cc) {
		this.cc = cc;
	}
	public int getContentType() {
		return contentType;
	}
	public void setContentType(int contentType) {
		this.contentType = contentType;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getBcc() {
		return bcc;
	}
	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public boolean isAuth() {
		return bAuth;
	}
	
	public void setAuth(boolean bAuth) {
		this.bAuth = bAuth;
	}
	
	public boolean isEncode() {
		return bEncode;
	}

	public void setEncode(boolean bEncode) {
		this.bEncode = bEncode;
	}
}
